/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import beans.BeanProyecto;
import dao.basedato.Conexion;
import dao.basedato.DaoProyecto;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
 *
 * @author Administrador
 */
public class ProyectoServlet extends HttpServlet {
    private DaoProyecto aproyecto;
    private BeanProyecto proyecto ;
    boolean resultado = false;
    private String mensaje = null;
    private HttpSession sesion;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String accion = request.getParameter("accion");

       if (accion != null) {
            if (accion.equals("insertar")) {
                insertar(request, response);
                return;
            }
            if (accion.equals("buscar")) {
                buscar(request, response);
                return;
            }
            if (accion.equals("obtenerPorId")) {
                obtenerPorId(request, response);
                return;
            }
            if (accion.equals("actualizar")) {
                actualizar(request, response);
                return;
            }
            if (accion.equals("eliminar")) {
                eliminar(request, response);
            }
        }
    }

    private void insertar(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id") == null ? "" : 
                request.getParameter("id"));
        String nombre = request.getParameter("nombre") == null ? "" : 
                request.getParameter("bombre");
        String cliente = request.getParameter("cliente") == null ? "" : 
                request.getParameter("cliente");
        String estado = request.getParameter("estado") == null ? "" : 
                request.getParameter("estado");

        try{
            proyecto = new BeanProyecto();
            proyecto.setId(id);
            proyecto.setNombre(nombre);
            proyecto.setCliente(cliente);
            proyecto.setEstado(estado);
            
            resultado = DaoProyecto.insertarProyecto(proyecto);
            if(resultado = true){
                mensaje = "Cliente insertado correctamente.";
            }else{
                mensaje = "Error al insertar cliente.";
            }
            sesion = request.getSession();
            sesion.removeAttribute("msgPostOperacion");
            sesion.removeAttribute("listaProyectos");
            sesion.removeAttribute("msgListado");
            sesion.removeAttribute("proyectoActualizar");
            sesion.setAttribute("msgPostOperacion", mensaje);
            buscar(request, response);
        }catch(Exception e){

        }
    }

    private void buscar(HttpServletRequest request, HttpServletResponse response) {

    }

    private void obtenerPorId(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void actualizar(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void eliminar(HttpServletRequest request, HttpServletResponse response) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
/**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
